<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
      class="toptitle animate__animated"
      :class="dynamicClassName"
      style="width: 1920px;height: 124px;"
      :id="dynamicChildName()"
    >
      <p class="top-nav top-left">
        <span>大厅运行</span>
        <span>业务办理</span>
      </p>
      <span class="toptitle-t">花都区政务服务专题分析</span>
      <p class="top-nav  top-right">
        <span class="active">服务能力</span>
        <span>服务评估</span>
      </p>
    </div>
  </div>
</template>
<script>
//引入封装方法
import encapsulation from "../../utils/aircimGird";
export default {
  //混入封装方法
  mixins: [encapsulation],
  data: function() {
    return {
      resizeObserver: null,
      //动态Id
      timestamp: null,
    };
  },
  mounted() {
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    initGrid() {},
    refreshConfig(val) {},
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
        `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.toptitle {
  width: 1920px;
  display: flex;
  justify-content: space-between;
  padding: 19px 18px 0 18px;
  height: 124px;
  background: url(../../static/assets/TopTitle/bg.png) no-repeat;
  background-size: 100% 124px;
  transform-origin: left top;
  .toptitle-t {
    color: #fff;
    text-align: center;
    font-family: "YouSheBiaoTiHei";
    font-size: 48px;
    font-style: normal;
    font-weight: 400;
    letter-spacing: 5px;
    height: 51px;
    line-height: 51px;
    background: linear-gradient(180deg, #fff 44.58%, #a8efff 64.86%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
}

.top-nav {
  width: 22.2%;
  display: inline-flex;
  font-size: 26px;
  font-style: normal;
  font-weight: 700;

  span {
    position: relative;
    text-align: center;
    width: 50%;
    height: 53px;
    line-height: 53px;
    background: linear-gradient(180deg, #ecf7ff 64.58%, #b6d0e2 64.86%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    cursor: pointer;

    &:first-of-type {
      margin-right: -24px;
    }

    &.active {
      background: linear-gradient(180deg, #ecf7ff 64.58%, #a8efff 64.86%);
      background-clip: text;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;

      &::after {
        background-image: url(../../static/assets/TopTitle/navactivebg.png);
      }
    }

    &::after {
      content: "";
      position: absolute;
      inset: 0;
      background-image: url(../../static/assets/TopTitle/navbg.png);
      background-size: 100% 100%;
      z-index: -1;
    }
  }

  &.top-right {
    span {
      &::after {
        transform: scale(-1);
      }
    }
  }
}
</style>
